package com.web.base.auth.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blue.jdbc.JdbcDataSource;
import com.web.base.auth.dao.MenuAuthDao;
import com.web.base.auth.dao.MenuDao;
import com.web.base.auth.dao.RoleMenuAuthDao;
import com.web.base.auth.model.MenuAuth;
import com.web.base.auth.model.RoleMenuAuth;
import com.web.core.util.Const;

/**
 * 菜单权限业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月15日
 */
@Service
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class MenuAuthService
{
	@Resource
	private MenuAuthDao menuAuthDao;
	@Resource
	private MenuDao menuDao;
	@Resource
	private RoleMenuAuthDao roleMenuAuthDao;
	
	public MenuAuthService()
	{
	}
	
	public MenuAuth save(MenuAuth menuAuth)
	{
		if (menuAuth.getId() == null)
		{
			menuAuthDao.save(menuAuth);
			menuDao.inc(menuAuth.getMenuId(), "auths", 1);
		}
		else
		{
			menuAuthDao.update(menuAuth);
		}
		MenuAuth a = menuAuthDao.get(menuAuth.getId());
		return a;
	}
	
	public MenuAuth get(Integer id)
	{
		MenuAuth log = menuAuthDao.get(id);
		return log;
	}
	
	public List<MenuAuth> list(MenuAuth menuAuth)
	{
		List<MenuAuth> list = menuAuthDao.list(menuAuth);
		return list;
	}
	
	public void delete(MenuAuth menuAuth)
	{
		roleMenuAuthDao.deleteBy("authId", menuAuth.getId());
		menuAuthDao.delete(menuAuth);
		menuDao.inc(menuAuth.getMenuId(), "auths", -1);
	}
	
	public List<RoleMenuAuth> listRoleMenuAuth(RoleMenuAuth roleMenuAuth)
	{
		List<RoleMenuAuth> list = roleMenuAuthDao.list(roleMenuAuth);
		return list;
	}
	
	public List<MenuAuth> listSelect(Integer roleId, Integer appId)
	{
		List<MenuAuth> list = roleMenuAuthDao.listSelect(roleId, appId);
		return list;
	}
	
}
